/**
 * 
 */
package com.questetra.bpms.gae.forms.client;

import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Tsuyoshi Kusaka
 */
public class MessageStartEventForm extends Composite {

    private static MessageStartEventFormUiBinder uiBinder = GWT
            .create(MessageStartEventFormUiBinder.class);

    interface MessageStartEventFormUiBinder extends
            UiBinder<Widget, MessageStartEventForm> {
    }

    @UiField
    Button btnSend;
    //TODO modify
    @UiField
    TextBox tbName;
    @UiField
    TextBox tbEmail;
    @UiField
    TextBox tbOutline;
    @UiField
    TextArea taDetails;

    private final MessageServiceAsync messageService = GWT.create(MessageService.class);

    private static class MyDialog extends DialogBox {
        public MyDialog() {
          // DialogBox is a SimplePanel, so you have to set its widget property to
          // whatever you want its contents to be.
          Button ok = new Button("OK");
          ok.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
              MyDialog.this.hide();
            }
          });
          setWidget(ok);
        }
      }

    public MessageStartEventForm() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiHandler("btnSend")
    void onClick(ClickEvent e) {
//        Window.alert("send QBPMS");

        //TODO modify
        final String name = tbName.getText();
        final String email = tbEmail.getText();
        final String outline = tbOutline.getText();
        final String details = taDetails.getText();

        //TODO validation
//        StringBuilder sb = new StringBuilder();
//        if (!FieldVerifier.isValidName(name)) {
//            sb.append("name length more than 3<br />");
//        }
//        if (!"".equals(sb.toString())) {
//            dialog.center(sb.toString());
//            return;
//        }

        MessageData data = new MessageData();
        Map<String, String> dataMap = data.getDataMap();
        //TODO modify
        dataMap.put("data[2].input", name);
        dataMap.put("data[3].input", email);
        dataMap.put("data[0].input", outline);
        dataMap.put("data[1].input", details);

        messageService.sendMessageData(data, new AsyncCallback<Void>() {
            
            @Override
            public void onSuccess(Void result) {
                MyDialog dialog = new MyDialog();
                dialog.setText("send success");
                dialog.show();
            }
            
            @Override
            public void onFailure(Throwable caught) {
                MyDialog dialog = new MyDialog();
                dialog.setText("send failure");
                dialog.show();
            }
        });
    }
}
